package com.yy.show.util;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.yy.base.util.StringUtil;
import com.yy.module.entity.ModuleField;
import com.yy.module.entity.ModuleInfo;
import com.yy.module.entity.ModuleQueryField;

/**
 * 树形模块处理类
 */
@SuppressWarnings("unchecked")
public class ModuleTreeHandle extends ModuleHandle {
	public ModuleTreeHandle(ModuleInfo module, HttpServletRequest request) {
		super(module, request);
	}
	
	public Map queryDataToMapTree(List<Map<String, Object>> list) {
		Map m = new HashMap();
		Map map = null;
		List<ModuleField> field = module.getFieldList();
		m.put("data", list);
		map = new HashMap();
		for (int i = 0, size = field.size(); i < size; i++) {
			if (field.get(i).getTreeType() != null) {
				map.put(field.get(i).getTreeType(), field.get(i).getTableName() + "_" + field.get(i).getFieldName());
			}
		}
		m.put("fields", map);
		return m;
	}
	/**
	 * 获取查询SQL
	 */
	public String getSql() {
		return sql.toString();
	}
	
	/**
	 * 判断参数格式是否正确
	 */
	public boolean hasError() {
		sql = new StringBuilder();
		sql.append("select ");
		List<ModuleField> list = module.getFieldList();
		ModuleField field = null;
		tables = new HashMap<String, String>();
		String t = null;
		String parentIdName = null;
		String idName = null;
		
		for (int i = 0, size = list.size(); i < size; i++) {
			field = list.get(i);
			t = tables.get(field.getTableName());
			if (t == null) {
				t = "t" + i;
				tables.put(field.getTableName(), t);
			}
			if (i != 0) {
				sql.append(",");
			}
			sql.append(t).append(".").append(field.getFieldName()).append(" ").append(field.getTableName()).append("_").append(field.getFieldName());
			
			if ("parentId".equals(field.getTreeType())) {
				parentIdName = field.getFieldName();
			} else if ("id".equals(field.getTreeType())) {
				idName = field.getFieldName();
			}
		}
		
		sql.append(" from ");
		int i = 0;
		for (String table : tables.keySet()) {
			if (i > 0) {
				sql.append(",");
			}
			sql.append(table).append(" ").append(tables.get(table));
			i ++;
		}
		sql.append(" where 1=1 ");
		//判断是否有多表关联
		if (tables.keySet().size() > 1) {
			
		}
		
		//查询条件
		values = new HashMap();
		List<ModuleQueryField> queryList = module.getQueryFieldList();
		Object value = null;
		String format = null;
		ModuleQueryField query = null;
		
		for (int index = 0; index < queryList.size(); index++) {
			query = queryList.get(index);
			//如果是隐藏的，并且有默认值，处理默认值
			if (query.getIsHide() == 1 && StringUtil.isNotEmpty(query.getDefaultValue())) {
				value = getDefaultValue(query.getDefaultValue());
				
				if (StringUtil.isEmpty(value)) {
					if (query.getIsNotNull() == 1) {
						error = "[" + query.getName() + "] 不能为空！";
						return true;
					}
				}
			} else {
				value = request.getParameter(query.getTableName() + "_" + query.getFieldName());
				if (value == null) {
					value = request.getParameter(query.getFieldName());
				}
				if (StringUtil.isEmpty(value)) {
					if (query.getIsNotNull() == 1) {
						error = "[" + query.getName() + "] 不能为空！";
						return true;
					}
					continue;
				}
			}
			if (StringUtil.isEmpty(value)) {
				sql.append(" and ").append(tables.get(query.getTableName())).append(".").append(query.getFieldName()).append(" is null");
			} if (ModuleField.DATA_VALUE_DATE.equals(query.getType())) {
				format = query.getFormat() != null && query.getFormat().length() > 7 ? query.getFormat() : "yyyy-MM-dd";
				try {
					value = new SimpleDateFormat(format).parse((String)value);
				} catch (Exception e) {
					error = query.getName() + "解析成时间格式 \"" + format + "\" 错误！";
					return true;
				}
				sql.append(" and ").append(tables.get(query.getTableName())).append(".").append(query.getFieldName()).append(" >= :p").append(index);
				values.put("p" + index, value);
			} else if (ModuleField.DATA_VALUE_NUMBER.equals(query.getType())) {
				sql.append(" and ").append(tables.get(query.getTableName())).append(".").append(query.getFieldName()).append(" >= :p").append(index);
					values.put("p" + index, value);
			} else {
				sql.append(" and ").append(tables.get(query.getTableName())).append(".").append(query.getFieldName()).append(" like :p").append(index);
				values.put("p" + index, "%" + value.toString() + "%");
			}
		}
		if (parentIdName == null || idName == null) {
			error = module.getName() + "未设置树形结构ID，父ID！";
			return true;
		}
		sql.append(" order by ").append(parentIdName).append(",").append(idName);
		return false;
	}
}